﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class BaoZhangCheLiangController : BaseController
    {
        private readonly ILogger<BaoZhangCheLiangController> _logger;
        public BaoZhangCheLiangController(ILogger<BaoZhangCheLiangController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index()
        {
            List<BaoZhangCheLiang> baozhangcheliangs = await _fsql.Select<BaoZhangCheLiang>().Count(out var total).ToListAsync();
            _logger.LogInformation(_fsql.Select<BaoZhangCheLiang>().ToSql());
            JObject data = new JObject();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(baozhangcheliangs));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] BaoZhangCheLiang baozhangcheliang)
        {
            long id = await _fsql.Insert(baozhangcheliang).ExecuteIdentityAsync();
            baozhangcheliang.Id = id;

            return RenderJson(data: baozhangcheliang);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id) {
            int affrows = await _fsql.Delete<BaoZhangCheLiang>().Where(bzcl => bzcl.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] BaoZhangCheLiang baozhangcheliang) {
            int affrows = await _fsql.Update<BaoZhangCheLiang>().SetSourceIgnore(baozhangcheliang, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id) {
            BaoZhangCheLiang baozhangcheliang = await _fsql.Select<BaoZhangCheLiang>().Where(bzcl => bzcl.Id == id).ToOneAsync();
            return RenderJson(data: baozhangcheliang);
        }
    }
}
